package com.itheima.contact.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.contact.dao.UserMapper;
import com.itheima.contact.domain.PageBean;
import com.itheima.contact.domain.User;
import com.itheima.contact.utils.SessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author Azer
 * @LOVE,,,
 * @Date 2021/9/19 10:14
 */
public class UserService {

    /**
     * 使用PageHelper实现分页查询
     * @param currentPage
     * @return
     */
    public PageInfo<User> findUserByPage(int currentPage){

        //每页显示的数据条数
        int pageSize=3;


        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //设置分页查询
        PageHelper.startPage(currentPage,pageSize);

        //查询数据
        List<User> users = userMapper.queryAllUsers();

        //释放资源
        sqlSession.close();

        //实例化PageInfo对象
        PageInfo<User> pageInfo=new PageInfo<>(users);

        return pageInfo;


    }

    /**
     * 根据搜索数据查询用户条件
     * @return
     */
    public List<User> findUserBySearch(String keyword){
        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.queryUserBySearch(keyword);
        sqlSession.close();

        return users;
    }


    /**
     * 将每一页要展示的数据和一些条件封装起来 PageBean
     * @param curPage
     * @param pageSize
     * @return
     */
    public PageBean queryPageBean(int curPage,int pageSize){
        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        PageBean pageBean=new PageBean();
        //添加数据
        pageBean.setCurPage(curPage);
        pageBean.setPageSize(pageSize);

        List<User> data = userMapper.queryData(pageBean.getStartIndex(), pageBean.getPageSize());

        //把一页要显示的数据放到PageBean的元素 专门存储的集合中
        pageBean.setData(data);

        //获取数据库中数据的条数
        int count = userMapper.queryCount();

        //放到PageBean中
        pageBean.setCount(count);

        //将实体类返回Servlet
        return pageBean;

    }

    /**
     * 修改用户信息
     *
     * @return
     */
    public void updateUserById(User user) {
        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        userMapper.updateById(user);

        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 根据id查询用户  用来回显数据
     *
     * @param id
     * @return
     */
    public User queryUserById(int id) {
        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = userMapper.queryById(id);

        return user;

    }

    /**
     * 删除用户
     *
     * @param userid
     * @return
     */
    public boolean deleteUserById(int userid) {

        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int i = userMapper.deleteUserById(userid);
        if(i>0){
            sqlSession.commit();
            sqlSession.close();
            return true;
        }else {
            sqlSession.rollback();
            sqlSession.close();
            return false;
        }



    }

    /**
     * 添加新用户
     *
     * @param user 用户对象
     * @return 用户添加结果
     */
    public boolean saveUser(User user) {
        //对User对象中的数据进行合法性验证
        if (user == null ||
                user.getName() == null || user.getName().equals("") ||
                user.getEmail() == null || user.getName().equals("")) {
            return false;
        }


        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //查询邮箱是否已被注册
        User user1 = userMapper.queryUserByEmail(user.getEmail());
        if (user1 != null) {
            //邮箱重复
            sqlSession.close();
            return false;
        } else {
            //没有该邮箱 可以注册
            int i = userMapper.addUser(user);
            if (i > 0) {
                //添加成功 提交
                sqlSession.commit();
                sqlSession.close();
            } else {
                //添加失败 事务回滚
                sqlSession.rollback();
            }
            return true;
        }

    }

    /**
     * 查询所有用户
     *
     * @return
     */
    public List<User> findAllUsers() {
        SqlSession sqlSession = SessionFactoryUtils.getSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.queryAllUsers();
        sqlSession.close();
        return userList;
    }
}
